//
//  OrderViewController.m
//  CenturyInterconnection
//
//  Created by l-sheng on 6/13/16.
//  Copyright © 2016 Shanghai DuRui Information Technology Company. All rights reserved.
//

#import "OrderViewController.h"
#import "orderTableViewCell.h"
#import "OrderHistoryModel.h"
#import "OrderDetailsViewController.h"

static NSString *CellIdentifier = @"CellIdentifier";

@implementation OrderViewController 


@synthesize myTableView;

- (UITableView *)myTableView{
    if(!myTableView){
        myTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        myTableView.frame = CGRectMake(0, 0, KScreenWidth, KScreenHeight);
        myTableView.delegate = self;
        myTableView.dataSource = self;
    }
    return myTableView;
}

#pragma mark - viewDidLoad
- (void) viewDidLoad
{
    self.title = @"Orders";
    
    self.current_page_number = @"1";
    self.order_history_arry = [NSMutableArray array];
    [self makeNavBtn];
    [self getOrderData];
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self.myTableView registerClass:[orderTableViewCell class] forCellReuseIdentifier:CellIdentifier];
    [self.view addSubview:myTableView];
    
}
-(void)makeNavBtn{
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setBackgroundImage:[UIImage imageNamed:@"nav_back"] forState:0];
    leftButton.frame=CGRectMake(0, 0, ACTUAL_WIDTH(23), ACTUAL_HEIGHT(23));
    [leftButton addTarget:self action:@selector(comeBack) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem*item= [[UIBarButtonItem alloc]initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem=item;
}
-(void)comeBack{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)getOrderData{
    
    // http://viprunningonline.com/api/index.php?main_page=account_history
    if ([UserSession instance].isLogin == YES) {
        NSString*urlStrr=[NSString stringWithFormat:@"%@main_page=account_history",HTTP_ADDRESS];
        NSDictionary*paramss=@{ @"zenid":[UserSession instance].cookie,
                                @"current_page_number":self.current_page_number};
        HttpManager*managerr=[[HttpManager alloc]init];
        
        [managerr postDataFromNetworkWithUrl:urlStrr parameters:paramss compliation:^(id data, NSError *error) {
            
            
            NSString*number=[NSString stringWithFormat:@"%@",data[@"status"]];
            
            if ([number isEqualToString:@"1"]) {
                
                self.orderData = data;
                [self.order_history_arry addObjectsFromArray:data[@"data"][@"order_history"]];
                
                [self.myTableView reloadData];
                
            }else{
                [JRToast showWithText:data[@"status_message"] duration:2.0];
            }
        }];
    }
    
    
    
}

#pragma mark - tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.order_history_arry.count;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return KScreenHeight*0.2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    orderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    
    if(cell == nil){
        cell =(orderTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
        
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.orderLabel.text = self.order_history_arry[indexPath.row][@"orders_id"];
    cell.timeLabel.text = self.order_history_arry[indexPath.row][@"date_purchased"];
    cell.NameLabel.text = self.order_history_arry[indexPath.row][@"billing_name"];
    cell.priceTotalLabel.text = self.order_history_arry[indexPath.row][@"order_total"];
    cell.statusLabel.text = self.order_history_arry[indexPath.row][@"orders_status_name"];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString * orderID = (NSString *) self.order_history_arry[indexPath.row][@"orders_id"];
    
    OrderDetailsViewController * orderDetailVC = [[OrderDetailsViewController alloc] init];
    orderDetailVC.orderID = orderID;
    
    [[SlideNavigationController sharedInstance] pushViewController:orderDetailVC animated:YES];
    
}


@end
